package com.siyoumi.app.modules.book.service;

import com.siyoumi.app.entity.BookSku;
import com.siyoumi.app.entity.BookSkuDay;
import com.siyoumi.app.modules.book.entity.BookSkuTime;
import com.siyoumi.app.modules.book.vo.VaBookSku;
import com.siyoumi.app.service.BookSkuService;
import com.siyoumi.component.XApp;
import com.siyoumi.component.XSpringContext;
import com.siyoumi.component.http.InputData;
import com.siyoumi.component.http.XHttpContext;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.service.IWebService;
import com.siyoumi.util.XDate;
import com.siyoumi.util.XReturn;
import com.siyoumi.util.XStr;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

//sku
@Slf4j
@Service
public class SvcBookSku
        implements IWebService {
    static public SvcBookSku getBean() {
        return XSpringContext.getBean(SvcBookSku.class);
    }

    static public BookSkuService getApp() {
        return BookSkuService.getBean();
    }

    public JoinWrapperPlus<BookSku> listQuery() {
        return listQuery(InputData.getIns());
    }

    /**
     * 当天截止时间
     *
     * @param entitySku
     * @param beforeMinute
     */
    public LocalDateTime getDateTimeEnd(BookSku entitySku, String date, Long beforeMinute) {
        BookSkuTime timeData = BookSkuTime.parse(entitySku.getBsku_name());
        LocalDateTime dateTime = XDate.parse(date + " " + timeData.getBeginTime() + ":00");
        //提交X分钟
        LocalDateTime dateTimeEnd = dateTime.minusMinutes(beforeMinute);
        return dateTimeEnd;
    }

    /**
     * select
     *
     * @return query
     */
    public JoinWrapperPlus<BookSku> listQuery(InputData inputData) {
        String bookId = inputData.input("book_id");
        String name = inputData.input("name");

        JoinWrapperPlus<BookSku> query = getApp().join();
        query.eq("bsku_x_id", XHttpContext.getX())
                .eq("bsku_del", 0)
        //.eq("book_del", 0)
        ;
        query.orderByAsc("bsku_order")
                .orderByAsc("bsku_create_date");
        if (XStr.hasAnyText(bookId)) { //ID
            query.eq("bsku_book_id", bookId);
        }
        if (XStr.hasAnyText(name)) {
            query.like("bsku_name", name);
        }

        return query;
    }

    /**
     * 编辑
     *
     * @param inputData
     * @param vo
     */
    @SneakyThrows
    public XReturn edit(InputData inputData, VaBookSku vo) {
        XReturn r = getApp().saveEntity(inputData, vo, true, null);
        return r;
    }

    /**
     * 删除
     */
    public XReturn delete(List<String> ids) {
        JoinWrapperPlus<BookSku> query = listQuery();
        query.in(BookSku.tableKey(), ids)
                .eq("bsku_del", 0);

        List<BookSku> list = getApp().get(query);

        XApp.getTransaction().execute(status -> {
            for (BookSku entity : list) {
                getApp().delete(entity.getKey());
            }

            //删除所有日期
            JoinWrapperPlus<BookSkuDay> queryDay = SvcBookSkuDate.getBean().listQuery();
            queryDay.in("bday_sku_id", ids);
            List<BookSkuDay> listDay = SvcBookSkuDate.getApp().get(queryDay);
            for (BookSkuDay entityDay : listDay) {
                SvcBookSkuDate.getApp().delete(entityDay.getKey());
            }
            return null;
        });

        return XReturn.getR(0);
    }
}
